import { Express, Response, Request } from "express";
import { addComment, readComments, getTotal } from "../model/comment";
import { comments } from "../tool/varType";
export default function (app: Express) {
  app.post("/api/addcomment", async function (req: Request, res: Response) {
    let comments: comments = req.body;
    const { success, data } = await addComment(comments);
    if (success) {
      res.json({ success: true, msg: "添加成功" });
      return;
    } else {
      res.json({ success: true, msg: "添加失败" });
      return;
    }
  });
  app.post("/api/getcomment", async function (req: Request, res: Response) {
    let { page, blog_id } = req.body;
    const { success, data } = await readComments(Number(page), blog_id);
    console.log(data, success);

    if (success) {
      const result = await getTotal(blog_id);
      if (success) {
        res.json({ success: true, data: data, total: result.data[0].total });
        return;
      }
    } else {
      res.json({ success: false, data: "获取失败" });
      return;
    }
  });
}
